<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://zotero/skin/errorReport.css" type="text/css"?>
<?xml-stylesheet href="chrome://zotero-platform/content/zotero.css"?>

<!DOCTYPE window SYSTEM "chrome://zotero/locale/zotero.dtd">

<window
	xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	xmlns:html="http://www.w3.org/1999/xhtml"
	onload="Zotero_Error_Report.init()">

<linkset>
	<html:link rel="localization" href="toolkit/global/wizard.ftl"/>
</linkset>

<script>
	var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
	Services.scriptloader.loadSubScript("chrome://zotero/content/include.js", this);
	Services.scriptloader.loadSubScript("chrome://zotero/content/customElements.js", this);
</script>

<wizard title="&zotero.errorReport.title;" width="550" height="450">
	<script>
	<![CDATA[
		Components.utils.import("resource://zotero/config.js");
		
		var Zotero_Error_Report = new function() {
			var obj = window.arguments[0].wrappedJSObject;
			var Zotero = obj.Zotero;
			var data = obj.data;
			var msg = data.msg;
			var errorData = data.errorData;
			var extraData = data.extraData ? data.extraData : '';
			var diagnosticInfo = false;
			
			
			this.init = Zotero.Promise.coroutine(function* () {
				// Allow Continue button to be populated
				yield Zotero.Promise.delay(100);
				
				var wizard = document.querySelector('wizard');
				var continueButton = wizard.getButton('next');
				continueButton.disabled = true;
				
				diagnosticInfo = yield Zotero.getSystemInfo();
				var errorDataText = errorData.length
					? data.errorData.join('\n\n')
					: Zotero.getString('errorReport.noErrorsLogged', Zotero.appName);
				
				var logText = errorDataText + '\n\n'
					+ (extraData !== '' ? extraData + '\n\n' : '')
					+ diagnosticInfo;
				
				if (document.getElementById('zotero-failure-message').hasChildNodes()) {
					var textNode = document.getElementById('zotero-failure-message').firstChild;
					document.getElementById('zotero-failure-message').removeChild(textNode);
				}
				
				document.getElementById('zotero-failure-message').appendChild(document.createTextNode(msg));
				document.getElementById('zotero-error-message').value = logText;
				
				continueButton.disabled = false;
				continueButton.focus();
				var str = Zotero.getString(
					'errorReport.advanceMessage', continueButton.getAttribute('label')
				);
				document.getElementById('zotero-advance-message').setAttribute('value', str);
				
				var pageSending = document.querySelector('wizardpage[pageid="report-sending"]');
				pageSending.addEventListener('pageshow', () => Zotero_Error_Report.sendErrorReport());
			});
			
			this.sendErrorReport = Zotero.Promise.coroutine(function* () {
				var wizard = document.querySelector('wizard');
				var continueButton = wizard.getButton('next');
				continueButton.disabled = true;
				
				var parts = {
					error: "true",
					errorData: errorData.join('\n'),
					extraData: extraData,
					diagnostic: diagnosticInfo
				};
				
				var body = '';
				for (var key in parts) {
					body += key + '=' + encodeURIComponent(parts[key]) + '&';
				}
				body = body.substr(0, body.length - 1);
				var req = yield Zotero.HTTP.request(
					"POST",
					ZOTERO_CONFIG.REPOSITORY_URL + "report",
					{
						body,
						successCodes: false,
						foreground: true
					}
				);
				_sendErrorReportCallback(req);
			});
			
			
			function _sendErrorReportCallback(xmlhttp) {
				var wizard = document.querySelector('wizard');
				if (!wizard) {
					return;
				}
				
				var ps = Services.prompt;
				
				if (!xmlhttp.responseXML){
					try {
						if (xmlhttp.status>1000){
							ps.alert(
								null,
								Zotero.getString('general.error'),
								Zotero.getString('errorReport.noNetworkConnection')
							);
						}
						else {
							ps.alert(
								null,
								Zotero.getString('general.error'),
								Zotero.getString('errorReport.invalidResponseRepository')
							);
						}
					}
					catch (e){
						ps.alert(
							null,
							Zotero.getString('general.error'),
							Zotero.getString('errorReport.repoCannotBeContacted')
						);
					}
					
					wizard.rewind();
					return;
				}
				
				
				var reported = xmlhttp.responseXML.getElementsByTagName('reported');
				if (reported.length != 1) {
					ps.alert(
						null,
						Zotero.getString('general.error'),
						Zotero.getString('errorReport.invalidResponseRepository')
					);
					wizard.rewind();
					return;
				}
				
				wizard.advance();
				wizard.getButton('cancel').disabled = true;;
				wizard.canRewind = false;
				var reportID = reported[0].getAttribute('reportID');
				document.getElementById('zotero-report-id').setAttribute('value', reportID);
				document.getElementById('zotero-report-result').hidden = false;
			}
		}
	]]>
	</script>
	
	<wizardpage
			id="report-start"
			pageid="report-start"
			next="report-sending"
			data-header-label-id="">
		<description id="zotero-failure-message"/>
		<html:textarea
				id="zotero-error-message"
				class="plain"
				readonly="true"
				multiline="true"
		/>
		<description id="zotero-advance-message"/>
	</wizardpage>
	
	<wizardpage
			pageid="report-sending"
			next="report-submitted"
			onpageshow="Zotero_Error_Report.sendErrorReport()"
			data-header-label-id="">
		<description>&zotero.errorReport.submissionInProgress;</description>
	</wizardpage>
	
	<wizardpage
			pageid="report-submitted"
			data-header-label-id="">
		<description>&zotero.errorReport.submitted;</description>
		<description id="zotero-report-result" hidden="true">
			&zotero.errorReport.reportID;
			<html:input id="zotero-report-id" type="text" class="plain" readonly="true"/>
		</description>
		<description>&zotero.errorReport.postToForums;</description>
		<description>&zotero.errorReport.notReviewed;</description>
	</wizardpage>
</wizard>
</window>